package com.sop.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.sop.dao.WatchArrangementsDao;
import com.sop.model.WatchArrangements;
import com.sop.model.vo.WatchArrangementsVo;
import com.sop.service.WatchArrangementsService;

import common.base.service.BaseServiceImpl;
import common.tools.CalendarUtil;
import common.tools.DateTimeTool;
import common.tools.StringTool;

@Service("watchArrangementsService")
@Transactional
public class WatchArrangementsServiceImpl extends BaseServiceImpl<WatchArrangements,WatchArrangementsVo> implements WatchArrangementsService{

	@Autowired
	private  WatchArrangementsDao  watchArrangementsDao;
	
	@Resource(name="watchArrangementsDao")
	public void setBaseDao(WatchArrangementsDao  watchArrangementsDao){
		super.setBaseDao(watchArrangementsDao);
	}
	
	@Override
	public Boolean deleteBatch(List<Integer> ids) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public WatchArrangements add(WatchArrangements entity) {
		watchArrangementsDao.save(entity);
		return get(entity.getId());
	}

	@Override
	public WatchArrangements update(WatchArrangements entity) {
		watchArrangementsDao.update(entity);
		return get(entity.getId());
	}

	@Override
	public  List<WatchArrangementsVo>   findByMonth(WatchArrangementsVo  watchArrangementsVo){
		List<WatchArrangementsVo> list = new ArrayList<WatchArrangementsVo>();
		Date  checkDate=watchArrangementsVo.getCheckDate();
		Date  startDate=CalendarUtil.getMonthStart(CalendarUtil.getYear(checkDate), CalendarUtil.getMonth(checkDate));
		Date  endDate=CalendarUtil.getMonthEnd(CalendarUtil.getYear(checkDate), CalendarUtil.getMonth(checkDate));
		Integer  startDateWeek = CalendarUtil.getWeekOfDate(startDate);
		Integer  endDateWeek = CalendarUtil.getWeekOfDate(endDate);
		startDate=CalendarUtil.getDateQianhou(startDate, -(startDateWeek==7?0:startDateWeek));
		endDate  =CalendarUtil.getDateQianhou(endDate, (endDateWeek==7?6:(6-endDateWeek)));
		for(;CalendarUtil.getMinusDay(endDate,startDate)>0;startDate=CalendarUtil.dateAdd(startDate)){
	        list.add(getNumber(startDate));	
	      }
		return  list;
	}
	
	@Override
	public List<WatchArrangementsVo>   findWeekByDate(WatchArrangementsVo  watchArrangementsVo){
		Date  checkDate=watchArrangementsVo.getCheckDate();
		List<WatchArrangementsVo> list = new ArrayList<WatchArrangementsVo>();
	    Integer  dateWeek = CalendarUtil.getWeekOfDate(checkDate);
	    Date  startDate=CalendarUtil.getDateQianhou(checkDate, -(dateWeek==7?0:dateWeek));
		Date  endDate  =CalendarUtil.getDateQianhou(checkDate, (dateWeek==7?6:(6-dateWeek)));
		for(;CalendarUtil.getMinusDay(endDate,startDate)>0;startDate=CalendarUtil.dateAdd(startDate)){
	        list.add(getDayArrange(startDate));	
	      }
		return list;
	}
	
	@Override
	public List<Date>   findWeek(Date checkDate){
		List<Date> list = new ArrayList<Date>();
	    Integer  dateWeek = CalendarUtil.getWeekOfDate(checkDate);
	    Date  startDate=CalendarUtil.getDateQianhou(checkDate, -(dateWeek==7?0:dateWeek));
		Date  endDate  =CalendarUtil.getDateQianhou(checkDate, (dateWeek==7?6:(6-dateWeek)));
		for(;CalendarUtil.getMinusDay(endDate,startDate)>0;startDate=CalendarUtil.dateAdd(startDate)){
	        list.add(startDate);	
	      }
		return list;
	}
	
	@Override
	public WatchArrangementsVo  getDayArrange(Date date){
		List<WatchArrangements>  list=findByDate(date);
		List<WatchArrangements>  normal=new ArrayList<WatchArrangements>();//普通班的人
		List<WatchArrangements>  early=new ArrayList<WatchArrangements>();//早班的人
		List<WatchArrangements>  evening=new ArrayList<WatchArrangements>();//晚班的人
		for(WatchArrangements  watchArrangements:list){
			if(StringTool.isStringAvaliable(watchArrangements.getNormal())){
				normal.add(watchArrangements);
			}
			if(StringTool.isStringAvaliable(watchArrangements.getEarly())){
				early.add(watchArrangements);
			}
			if(StringTool.isStringAvaliable(watchArrangements.getEvening())){
				evening.add(watchArrangements);
			}
		}
		return  new WatchArrangementsVo(date, normal, early, evening);
	}
	
	public  WatchArrangementsVo  getNumber(Date date){
		List<WatchArrangements>  list=findByDate(date);
		Integer  normalNumber=0;
		Integer  earlyNumber=0;
		Integer  eveningNumber=0;
		for(WatchArrangements  watchArrangements:list){
			if(StringTool.isStringAvaliable(watchArrangements.getNormal())){
				normalNumber++;
			}
			if(StringTool.isStringAvaliable(watchArrangements.getEarly())){
				earlyNumber++;
			}
			if(StringTool.isStringAvaliable(watchArrangements.getEvening())){
				eveningNumber++;
			}
		}
		return  new WatchArrangementsVo(date, normalNumber, earlyNumber, eveningNumber);
	}
	
	@Override
	public List<WatchArrangements> findByDate(Date date) {
		return watchArrangementsDao.findByDate(date);
	}

	@Override
	public boolean changeArrangement(WatchArrangementsVo watchArrangementsVo) throws Exception {
		WatchArrangements watchArrangementsOne = get(watchArrangementsVo.getChangeIdOne());
		WatchArrangements watchArrangementsTwo = get(watchArrangementsVo.getChangeIdTwo());
		String  replaceManOne=StringTool.isStringAvaliable(watchArrangementsOne.getReplaceMan())?watchArrangementsOne.getReplaceMan()
				:DateTimeTool.formatDateTimetoString6(watchArrangementsOne.getTime())+" "+watchArrangementsOne.getPerson().getName();
		String  replaceManTwo=StringTool.isStringAvaliable(watchArrangementsTwo.getReplaceMan())?watchArrangementsTwo.getReplaceMan()
				:DateTimeTool.formatDateTimetoString6(watchArrangementsTwo.getTime())+" "+watchArrangementsTwo.getPerson().getName();
		watchArrangementsOne.setReplaceMan(replaceManTwo);
		watchArrangementsTwo.setReplaceMan(replaceManOne);
		watchArrangementsDao.updateReplaceMan(watchArrangementsOne);
		watchArrangementsDao.updateReplaceMan(watchArrangementsTwo);
		
		//验证是否修改成功
	    watchArrangementsOne = get(watchArrangementsVo.getChangeIdOne());
	    watchArrangementsTwo = get(watchArrangementsVo.getChangeIdTwo());
		String  replaceManOneAfter=watchArrangementsOne.getReplaceMan();
		String  replaceManTwoAfter=watchArrangementsTwo.getReplaceMan();
		if(replaceManOneAfter.equals(replaceManTwo)||!replaceManTwoAfter.equals(replaceManOne)){
				throw new Exception();
		}
		return  true;
	}

}
